<?php
class GoodsModel extends viewModel{
        public $table="Goods";
        public $validate=array(
              array("gname","nonull","商品不能为空",2,3),
              array("cid","nonull","所属栏目不能为空",2,3)
        	);
        public $view=array(
              "category"=>array(
                        "type"=>INNER_JOIN,
                         "on"=>"goods.cid=category.cid"
              	)
        	);
        public $auto=array(
               array("addtime","strtotime","function",1,3),
               array("flag","flagAuto","method",1,3),
               array("index_pic","indexPicAuto","method",2,3),
               array("list_pic","listPicAuto","method",2,3)
        	);
        //验证属性
        public function flagAuto($v){
        	return implode(",", $v);
        }
         //验证首页图片
         public function indexPicAuto($v){
               if(!empty($_FILES['index_pic']['name'])){
               	$upload=new upload();
               	$info=$upload->upload('index_pic');
               	return $info[0]['path'];
              
               }
               return $this->data["index_pic"];
         }
         //验证列表图片
         public function listPicAuto($v){
                       if(!empty($_FILES['list_pic']['name'])){
               	$upload=new upload();
               	$info=$upload->upload('list_pic');
               	return $info[0]['path'];
         }
           return $this->data['list_pic'];
      }


         //增加商品
       public  function addGoods(){
        	if($this->create()){
               $state=$this->add();
                 if($state){
                      //上传内容图片
                      $upload=new Upload();;
                     $FilesInfo=$upload->upload('pic');
                     //插入内容图片
                     $img=new image();
                      foreach($FilesInfo as $pic){
                         $smallName=$pic['dir'].$pic['filename']."_200X200.".$pic['ext'];
                         $mediumName=$pic['dir'].$pic['filename']."_500X500.".$pic['ext'];
                         $bigName=$pic['dir'].$pic['filename']."_800X800.".$pic['ext'];
                         $smallPic=$img->thumb($pic['path'],$smallName,200,200);//这句话是为了生成缩略图
  
                         $mediumPic=$img->thumb($pic['path'],$mediumName,500,500);
                         $bigPic=$img->thumb($pic['path'],$bigName,800,800);
                         M("goods_pic")->add(array(
                                 "big"=>$bigName,
                                 "small"=>$smallName,
                                 "medium"=>$mediumName,
                                 "goods_gid"=>$state
                          ));
                          unlink($pic['path']);
                      }
                      //添加商品的属性值
                      $attrValue=$this->addAttr();
                      //往属性表里面添加数据
                      if($attrValue){
                            foreach($attrValue as $attr){
                        $attr["gid"]=$state;
                        $attr['cid']=Q("cid");
                        M("goods_attr")->add($attr);
                      }
                      //往库存里面添加数据
                      $this->addStock($state);
                        return true;
                      }
                 }else{
                           return false;
                 }
        	}else{
        	     return false;
        	}
        }
        //添加商品普通属性值
        public function addAttr(){
            $attrData=Q("attr");
            if(empty($attrData))return ;
             foreach($attrData as $name=>$value){
              if(strpos($name,"|")){
                $attr=explode("|",$name);
                $attrValue[$name]['avid']=$attr[1];
                $attrValue[$name]['value']=$value;
              }else{
                 $attr=explode("|",$value);
                 $attrValue[$name]['avid']=$attr[0];
                 $attrValue[$name]['value']=$attr[1];
              }
             }
             return $attrValue;
        }
//往库存里面添加数据
   public function addStock($state){
             //往库存表里面插入数据
                        $stockData=array();
                        $stockData['stock']=$_POST['stock'];
                        $stockData['price']=$_POST['price'];
                        $stockData['goods_sn']=$_POST['goods_sn'];
                        $stock=array();
                        $data=array();
                        foreach ($stockData['stock'] as $k=>$v ){
                             $data['stock']=$stockData['stock'][$k];
                             $data['price']=$stockData['price'][$k];
                             $data['goods_sn']=$stockData['goods_sn'][$k];
                             $stock[]=$data;
                        }
                          //循环往库存当中插入数据
                        foreach($stock as $k=>$v){
                             $value["goods_gid"]=$state;
                             //往库存表当中插入数据
                             $stockState=M("stock")->add($value);
                             //往内容属性表里面插入数据
                            $stock=array();
                             foreach($_POST['spec'] as $m){
                                //每往库存里面添加一条数据的时候，相应的添加里面的属性
                                   $stock=$m[$k];//对应每条库存里面的属性都处来了
                                    $arr=explode("|",$stock);
                                    $GoodsAttr=array("value"=>$arr[1],"cid"=>Q("cid"),'gid'=>$stockState,'avid'=>$arr[0]);
                                    M("goods_attr")->add($GoodsAttr);
                                    $StockAttr=array(
                                         "category_cid"=>Q("cid"),
                                         "goods_gid"=>$state,
                                         "stock_st_id"=>$stockState,
                                         "attr_value_av_id"=>$arr[0]
                                      );
                                    M("stock_attr")->add($StockAttr);
                              }
                          }


   }

        //编辑商品
        public function editGoods(){
           $pic_id=Q("gid");
        	if($this->create()){
        		if($this->save()){
                           //上传内容图片
                      $upload=new Upload();
                     $FilesInfo=$upload->upload('pic');
                     //插入内容图片
                     $img=new image();
                      foreach($FilesInfo as $pic){
                         $smallName=$pic['dir'].$pic['filename']."_200X200.".$pic['ext'];
                         $mediumName=$pic['dir'].$pic['filename']."_500X500.".$pic['ext'];
                         $bigName=$pic['dir'].$pic['filename']."_800X800.".$pic['ext'];
                         $smallPic=$img->thumb($pic['path'],$smallName,200,200);//这句话是为了生成缩略图
                         $mediumPic=$img->thumb($pic['path'],$mediumName,500,500);
                         $bigPic=$img->thumb($pic['path'],$bigName,800,800);
                         M("goods_pic")->add(array(
                                 "big"=>$bigName,
                                 "small"=>$smallName,
                                 "medium"=>$mediumName,
                                 "goods_gid"=>$pic_id
                          ));
                         unlink($pic['path']);
                      }
                      return true;
              	}else{
              		return false;
              	}
        }
}

        //删除内容
        public function delGoods(){
             if(M("Goods")->where(array("gid"=>Q("gid")))->del()){
                      return true;
             }else{
                      return false;
             }
        }




//删除商品内容图片
    public function delContentPic(){
          $pic_id=$_POST['pic_id'];
          $Model=M("goods_pic");
          $data=$Model->where(array("pic_id"=>$pic_id))->find();
          if($Model->where(array("pic_id"=>$pic_id))->del()){
             unlink($data['small']);
             unlink($data['medium']);
             unlink($data['big']);
             return true;
          }else{
            return false;
          }
}


}




